package cn.iocoder.yudao.module.fm.controller.admin.fileinfourl;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.fm.controller.admin.fileinfourl.vo.*;
import cn.iocoder.yudao.module.fm.dal.dataobject.fileinfourl.FileInfoUrlDO;
import cn.iocoder.yudao.module.fm.convert.fileinfourl.FileInfoUrlConvert;
import cn.iocoder.yudao.module.fm.service.fileinfourl.FileInfoUrlService;

@Tag(name = "管理后台 - 文件信息")
@RestController
@RequestMapping("/fm/file-info-url")
@Validated
public class FileInfoUrlController {

    @Resource
    private FileInfoUrlService fileInfoUrlService;

    @PostMapping("/create")
    @Operation(summary = "创建文件信息")
    @PreAuthorize("@ss.hasPermission('fm:file-info-url:create')")
    public CommonResult<Long> createFileInfoUrl(@Valid @RequestBody FileInfoUrlCreateReqVO createReqVO) {
        return success(fileInfoUrlService.createFileInfoUrl(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件信息")
    @PreAuthorize("@ss.hasPermission('fm:file-info-url:update')")
    public CommonResult<Boolean> updateFileInfoUrl(@Valid @RequestBody FileInfoUrlUpdateReqVO updateReqVO) {
        fileInfoUrlService.updateFileInfoUrl(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fm:file-info-url:delete')")
    public CommonResult<Boolean> deleteFileInfoUrl(@RequestParam("id") Long id) {
        fileInfoUrlService.deleteFileInfoUrl(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fm:file-info-url:query')")
    public CommonResult<FileInfoUrlRespVO> getFileInfoUrl(@RequestParam("id") Long id) {
        FileInfoUrlDO fileInfoUrl = fileInfoUrlService.getFileInfoUrl(id);
        return success(FileInfoUrlConvert.INSTANCE.convert(fileInfoUrl));
    }

    @GetMapping("/list")
    @Operation(summary = "获得文件信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fm:file-info-url:query')")
    public CommonResult<List<FileInfoUrlRespVO>> getFileInfoUrlList(@RequestParam("ids") Collection<Long> ids) {
        List<FileInfoUrlDO> list = fileInfoUrlService.getFileInfoUrlList(ids);
        return success(FileInfoUrlConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件信息分页")
    @PreAuthorize("@ss.hasPermission('fm:file-info-url:query')")
    public CommonResult<PageResult<FileInfoUrlRespVO>> getFileInfoUrlPage(@Valid FileInfoUrlPageReqVO pageVO) {
        PageResult<FileInfoUrlDO> pageResult = fileInfoUrlService.getFileInfoUrlPage(pageVO);
        return success(FileInfoUrlConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件信息 Excel")
    @PreAuthorize("@ss.hasPermission('fm:file-info-url:export')")
    @OperateLog(type = EXPORT)
    public void exportFileInfoUrlExcel(@Valid FileInfoUrlExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FileInfoUrlDO> list = fileInfoUrlService.getFileInfoUrlList(exportReqVO);
        // 导出 Excel
        List<FileInfoUrlExcelVO> datas = FileInfoUrlConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "文件信息.xls", "数据", FileInfoUrlExcelVO.class, datas);
    }

}
